/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.weapons.tool;

import com.minecraft.moonlake.MoonLakeAPI;
import com.minecraft.moonlake.api.packet.wrapper.PacketPlayOutChat;
import com.minecraft.moonlake.executor.Consumer;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponAbstractPlayerInteract;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponCloseable;
import com.minecraft.moonlake.util.StringUtil;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitTask;

import java.util.HashMap;
import java.util.Map;

public class Deal extends WeaponAbstractPlayerInteract implements WeaponCloseable {

    public Deal() {
        super("deal", "&b西蒙的交易", Material.GOLD_SWORD);
    }

    @Override
    public String[] getLore() {
        return new String[] {
                "&7右键使用后武器直接消失",
                "&7并给予&a速度2、力量1 60s &7效果",
                "&7一分钟后自身将被闪电劈中并爆炸"
        };
    }

    // 交易任务 map
    private final static Map<String, BukkitTask> DEAL_TASK_MAP = new HashMap<>();

    @Override
    protected void onInteract(PlayerInteractEvent event, Player player) {
        // 清除玩家手中的武器
        player.setItemInHand(new ItemStack(Material.AIR));
        playerUpdateInventory(player);
        // 给予效果
        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 60 * 20, 1));
        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 60 * 20, 0));
        // 发送警告消息
        String message = getInstance().getMessage("DealDelayEffectBombWarn", displayName, 60);
        new PacketPlayOutChat(message, PacketPlayOutChat.Mode.HOTBAR).send(player);
        // 清除旧 task 缓存
        removeTask(player);
        // 新建 task
        BukkitTask task = MoonLakeAPI.runTaskLater(getInstance(),  () -> {
            if(!player.isOnline()) return; // 玩家不在线则返回
            // 播放效果并杀死玩家
            player.getWorld().strikeLightningEffect(player.getLocation());
            player.getWorld().playSound(player.getLocation(), Sound.AMBIENCE_THUNDER, 10f, 1f);
            player.getWorld().playSound(player.getLocation(), Sound.EXPLODE, 10f, 1f);
            player.setHealth(0d);
        }, 60 * 20L);
        // 将 task put 到缓存 map
        DEAL_TASK_MAP.put(player.getName(), task);
    }

    // 玩家退出事件
    public Consumer<PlayerQuitEvent> onQuit(Player player) {
        return event -> removeTask(player);
    }

    // 玩家死亡事件
    public Consumer<PlayerDeathEvent> onDeath(Player player) {
        return event -> removeTask(player);
    }

    private void removeTask(Player player) {
        // 清除指定玩家的 task
        if(DEAL_TASK_MAP.containsKey(player.getName()))
            DEAL_TASK_MAP.remove(player.getName()).cancel();
    }

    @Override
    public void close() {
        // 关闭能力清除 task 缓存
        DEAL_TASK_MAP.values().stream().filter(StringUtil::isNotNull).forEach(BukkitTask::cancel);
        DEAL_TASK_MAP.clear();
    }
}
